package com.quicklife.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.quicklife.pojo.ActionLogs;
import com.quicklife.pojo.Coupons;
import com.quicklife.service.inter.CouponService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 安卓---优惠
 * 
 * 
 * @author HackerD
 * 
 */
public class CouponServiceImpl implements CouponService {

	@Override
	public String getHotCoupons(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,a.* from (select id,t.name,c.type typename,"
									+ "b.name busname,context,area,to_char(pub_time,'yyyy-MM-dd HH:mm:ss') pub_time,"
									+ "to_char(beg_time,'yyyy-MM-dd') beg_time,to_char(end_time,'yyyy-MM-dd') end_time,image,uses,t.score,t.status"
									+ " from coupons t inner join business b on t.busid=b.id inner join coupon_type c on t.type=c.id "
									+ "where t.status=1 and uses>0 order by uses desc) a) where rnum>=? and rnum<?",
							new String[] { StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getSusans(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String userid = jsonObject.getString("userid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,a.* from (select id,t.name,c.type typename,"
									+ "b.name busname,context,area,to_char(pub_time,'yyyy-MM-dd HH:mm:ss') pub_time,"
									+ "to_char(beg_time,'yyyy-MM-dd') beg_time,to_char(end_time,'yyyy-MM-dd') end_time,image,t.score "
									+ "from coupons t inner join business b on t.busid=b.id inner join coupon_type c on t.type=c.id "
									+ "where t.status=1 and t.id in(select couponid from sharecoupons where  instr(','||friends||',',','||?||',')>0))a) "
									+ "where rnum>=? and rnum<?", new String[] {
									userid, StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getNewCouponTip(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String userId = jsonObject.getString("userid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,a.* from (select t.id,t.name,c.type typename,"
									+ "b.name busname,context,area,to_char(pub_time,'yyyy-MM-dd HH:mm:ss') pub_time,"
									+ "to_char(beg_time,'yyyy-MM-dd') beg_time,to_char(end_time,'yyyy-MM-dd') end_time,image,uses,t.score,t.status "
									+ "from coupons t inner join business b on t.busid=b.id inner join coupon_type c on t.type=c.id "
									+ "inner join (select busid,max(pub_time) pub_time from coupons "
									+ "where busid in(select busid from favorite where userid=?) group by busid) d "
									+ "on t.busid=d.busid and t.pub_time=d.pub_time "
									+ "where t.status=1) a) where rnum>=? and rnum<?",
							new String[] { userId, StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getCouponDetail(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String cpid = jsonObject.getString("cpid");

			list = SqlHelper
					.executeQuery(
							"select t.id,t.name,c.type typename,b.name busname,context,area,"
									+ "to_char(pub_time,'yyyy-MM-dd HH:mm:ss') pub_time,"
									+ "to_char(beg_time,'yyyy-MM-dd') beg_time,"
									+ "to_char(end_time,'yyyy-MM-dd') end_time,"
									+ "image,uses,t.score,t.status from coupons t "
									+ "inner join business b on t.busid=b.id "
									+ "inner join coupon_type c on t.type=c.id where t.id=?",
							new String[] { cpid }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String useCoupon(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			String cpid = jsonObject.getString("cpid");
			String busid = jsonObject.getString("busid");

			// 查询最后一次使用是什么时候
			String last = "select  (sysdate-max(ac_time))*24 count from action_logs t where couponid=? and userid=? and ac_type=2";
			List list = SqlHelper.executeQuery(last, new String[] { cpid, id },
					null);
			Map map = (Map) list.get(0);
			if (map.get("count") != null
					&& ((BigDecimal) map.get("count")).doubleValue() > 0
					&& ((BigDecimal) map.get("count")).doubleValue() < 6) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"6小时内只能使用一次");
			}

			// 插入SQL语句
			String sql = "insert into action_logs(id,userid,couponid,ac_type,ac_time) "
					+ "values(?,?,?,2,sysdate)";
			SqlHelper.executeUpdate(
					sql,
					new String[] {
							StringUtil.toString(SqlHelper
									.getTableId("action_logs")), id, cpid });

			// 查询最后一次签到是什么时候
			last = "select (sysdate-max(ac_time))*24 count from action_logs t where bussid=? and userid=? and ac_type=1";
			list = SqlHelper.executeQuery(last, new String[] { busid, id },
					null);
			map = (Map) list.get(0);
			if (map.get("count") == null
					|| ((BigDecimal) map.get("count")).doubleValue() > 6) {
				// 插入SQL语句
				sql = "insert into action_logs(id,userid,bussid,ac_type,ac_time,comments) "
						+ "values(?,?,?,1,sysdate,?)";
				SqlHelper.executeUpdate(
						sql,
						new String[] {
								StringUtil.toString(SqlHelper
										.getTableId("action_logs")), id, busid,
								"系统自动签到" });
			}

			// 更新我的积分
			String updateSql = "update tuser set score=score+10,coupons=coupons+1 where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { id });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					"使用成功,积分+10");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String commCoupon(String rqinfos) {
		// TODO 拍照、录像、录音上传问题

		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			String cpid = jsonObject.getString("cpid");
			String comct = jsonObject.getString("comct");
			String score = jsonObject.getString("score");

			// 查询最后一次评价是什么时候
			String last = "select (sysdate-max(ac_time))*24 count from action_logs t where couponid=? and userid=? and ac_type=3";
			List list = SqlHelper.executeQuery(last, new String[] { cpid, id },
					null);
			Map map = (Map) list.get(0);
			if (map.get("count") != null
					&& ((BigDecimal) map.get("count")).doubleValue() > 0
					&& ((BigDecimal) map.get("count")).doubleValue() < 6) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"6小时内只能评价一次");
			}

			// 插入SQL语句
			String sql = "insert into action_logs(id,userid,couponid,ac_type,ac_time,score,comments) "
					+ "values(?,?,?,3,sysdate,?,?)";
			SqlHelper.executeUpdate(
					sql,
					new String[] {
							StringUtil.toString(SqlHelper
									.getTableId("action_logs")), id, cpid,
							score, comct });

			// 更新优惠评分
			String updateScore = "update coupons t set score=(select round(avg(score)) "
					+ "from action_logs where couponid=t.id) where t.id=?";
			SqlHelper.executeUpdate(updateScore, new String[] { cpid });

			// 更新我的积分
			String updateSql = "update tuser set score=score+5,comments=comments+1 where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { id });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					"评价成功,积分+5");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String shareConpons(String rqinfos) {
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			String cpid = jsonObject.getString("cpid");
			String friends = jsonObject.getString("friends");

			// 插入SQL语句
			String sql = "insert into sharecoupons(userid,couponid,friends,sharetime) values(?,?,?,sysdate)";
			SqlHelper.executeUpdate(sql, new String[] { id, cpid, friends });

			// 更新我的积分
			String updateSql = "update tuser set score=score+5 where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { id });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					"分享成功,积分+5");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getCouponComms(JSONObject jsonObject) {
		List list = null;
		try {
			int id = jsonObject.getInt("cpid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,f.* from (select b.nickname,"
									+ "b.photo uphoto,to_char(a.ac_time,'yyyy-MM-dd HH:mm:ss') ac_time,a.comments from action_logs a  "
									+ "inner join tuser b on a.userid=b.id "
									+ "where  a.ac_type=3 and a.couponid=? "
									+ "order by a.ac_time) f) "
									+ "where rnum>=? and rnum<?",
							new String[] { StringUtil.toString(id),
									StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			String rtInfos = JsonUtil.createRtJsonStr(
					StringUtil.RESULT_CODE_OK, list);

			return rtInfos;
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}
}
